package br.com.findme.findmeserver.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SenhaUtils {

	public static String encodeMD5(String s) throws NoSuchAlgorithmException{
				
		String md5 = null;
					
		//Create MessageDigest object for MD5
		MessageDigest digest = MessageDigest.getInstance("MD5");
		
		//Update input string in message digest
		digest.update(s.getBytes(), 0, s.length());

		//Converts message digest value in base 16 (hex) 
		md5 = new BigInteger(1, digest.digest()).toString(16);

		return md5;
	}
	
	public static String gerarSenha(int length){
		
		char[] characters = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',   
				'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A',   
		            'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',   
		        'W', 'X', 'Y', 'Z'};  
		  
		String tmp = "";  
		
		length = length < 4 ? 4 : length;
		
        for (int i = 0; i < length; i++) {  
              
            int c = (int) Math.floor(Math.random() * characters.length);  
              
            tmp += characters[c];  
              
        }  
          
        return tmp;  
		          
	}  

}
